<style lang="scss" scoped>
.inputNumber{
  color: red;
  ::v-deep{
    input{
      color: red;
    }
  }
}
</style>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="园区" prop="location">
        <el-select v-model="queryParams.location" placeholder="请选择园区" clearable style="width: 240px" @change="handleQuery">
          <el-option label="全部" value="" ></el-option>
          <el-option v-for="item in dict.type.user_area" :label="item.label" :value="item.value" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户昵称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入用户昵称"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="住户手机" prop="phone">-->
<!--        <el-input-->
<!--          v-model="queryParams.phone"-->
<!--          placeholder="请输入住户手机"-->
<!--          clearable-->
<!--          style="width: 200px"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="门牌号" prop="houseNumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.houseNumber"-->
<!--          placeholder="请输入住户门牌号"-->
<!--          clearable-->
<!--          style="width: 200px"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item >
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="customerList">
      <el-table-column label="用户编号" prop="customerId" align="center" width="150" fixed="left"/>
      <el-table-column label="用户昵称" prop="customerName" align="center" min-width="150"/>
      <el-table-column label="余额" prop="balance" align="center" width="150">
        <template v-slot="{row}">
          <span style="font-weight: bolder">{{row.balance | currency}}</span>
        </template>
      </el-table-column>
      <el-table-column label="园区" prop="location" align="center" width="150"/>
      <el-table-column label="联系电话" prop="phone" align="center" min-width="150"/>
<!--      <el-table-column label="门牌号" prop="houseNumber" align="center" width="150"/>-->
      <el-table-column label="创建人" prop="createBy" width="120" align="center"/>
      <el-table-column label="更新人" prop="updateBy" width="120" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160" fixed="right">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改信息</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleBalanceUpdate(scope.row)">余额调整</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleBalanceDetail(scope.row)">明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
      <el-col :span="12">
        <el-form-item v-if="form.id" label="住户编号" prop="id">
          <el-input :disabled="form.id" v-model="form.id"/>
        </el-form-item>
        <el-form-item label="住户名称" prop="customerName">
          <el-input :disabled="form.id" v-model="form.customerName" placeholder="请输入住户名称" />
        </el-form-item>
        <el-form-item label="所属区域" prop="location">
          <el-select v-model="form.location" placeholder="请选择地区" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in locationOptions" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="name">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="门牌号" prop="name">
          <el-input v-model="form.houseNumber" placeholder="请输入门牌号" />
        </el-form-item>
        <el-form-item label="备注" prop="name">
          <el-input v-model="form.extra" placeholder="请输入备注" />
        </el-form-item>
      </el-col>
    </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openBalance" width="500px" append-to-body>
      <el-form ref="balanceForm" :model="balanceForm" :rules="balanceRules" label-width="100px">
        <el-form-item label="操作类型" prop="type">
          <el-select v-model="balanceForm.type" placeholder="请选择操作类型" clearable :style="{width: '100%'}" @change="typeChange">
            <el-option v-for="(item, index) in typeOptions" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input-number v-model="balanceForm.amount" :class="balanceForm.type===2 ?'inputNumber':''" placeholder="金额" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="变更原由" prop="reason" >
          <el-select v-model="balanceForm.reason" placeholder="请选择变更原由" clearable style="width: 100%" v-if="balanceForm.type===1">
            <el-option v-for="item in dict.type.balance_add_reason" :label="item.label" :value="item.value" ></el-option>
          </el-select>
          <el-select v-model="balanceForm.reason" placeholder="请选择变更原由" clearable style="width: 100%" v-if="balanceForm.type===2">
            <el-option v-for="item in dict.type.balance_deduct_reason" :label="item.label" :value="item.value" ></el-option>
          </el-select>
<!--          <el-input v-model="balanceForm.reason" type="textarea" placeholder="请输入变更原由" :maxlength="512"-->
<!--                    :autosize="{minRows: 1, maxRows: 4}" :style="{width: '100%'}"></el-input>-->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitBalanceForm">确 定</el-button>
        <el-button @click="cancelBalance">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNormal, getNormal, addNormalCustomer, updateNormalBalance, updateNormalCustomer, delNormalCustomer } from "@/api/shop/customer";
import moment from "moment";
export default {
  name: "Normal",
  dicts:['user_area','balance_add_reason','balance_deduct_reason'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 食材表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      typeOptions: [{
        "label": "充值",
        "value": 1
      }, {
        "label": "扣钱",
        "value": 2
      }],
      locationOptions: [{
        "label": "二区",
        "value": "二区"
      }, {
        "label": "一区",
        "value": "一区"
      }],
      // 是否显示弹出层
      open: false,
      openBalance: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName:'',
        location:'',
      },
      // 表单参数
      form: {
        location: "二区"
      },
      balanceForm: {
        type: 1,
        amount: 100,
        reason: undefined,
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
      },
      balanceRules: {
        type: [{
          required: true,
          message: '请选择操作类型',
          trigger: 'change'
        }],
        amount: [{
          required: true,
          message: '金额',
          trigger: 'blur'
        }],
        reason: [{
          required: true,
          message: '请选择变更原由',
          trigger: 'change'
        }]
      }
    };
  },
  created() {
    this.queryParams.location=this.$store.state.user.userInfo.area || ''
    this.getList();
  },
  methods: {
    typeChange(){
      this.balanceForm.reason=''
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listNormal(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.customerList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelBalance() {
      this.openBalance = false;
      this.resetBalance();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    resetBalance() {
      this.resetForm("balanceForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form = {};
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getNormal(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
      });
    },
    handleBalanceUpdate(row) {
      this.resetBalance();
      this.openBalance = true;
      this.balanceForm.id = row.customerId;
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateNormalCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNormalCustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitBalanceForm: function() {
      this.$refs["balanceForm"].validate(valid => {
        if (valid) {
          updateNormalBalance(this.balanceForm).then(res => {
            this.$modal.msgSuccess("修改成功,即将前往小票打印");
            this.openBalance = false;
            this.getList();
            this.rowExport(res.data)
          });
        }
      })

    },
    //打印小票
    rowExport(id){
      this.$router.push({
        path:'/jmreport/consumption',
        query:{
          id:'931398146240327680',
          rowId:id,
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除编号为"' + row.id + '"的数据项？').then(function() {
        return delNormalCustomer(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleBalanceDetail(row){
      this.$router.push({
        path:'/customer/normal/detail',
        query:{
          uid:row.customerId
        }
      })
    },
    handleExport() {
      this.download('/shop/customer/export', {
        ...this.queryParams
      }, `用户信息${moment().format('YYYY-MM-DD')}.xlsx`)
    },
  }
};
</script>
